
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>下载app</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
  <style>
  </style>
</head>
<body>

<div class="layui-fluid">

  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto">
      <blockquote class="layui-elem-quote">
        App打开时调用，黑名单、无归属用户、疑似审核员看不到弹窗，读者符合多个下载条件则下载权重最高的app，下载完成后读者自动弹出提醒安装弹窗！如果取消弹出，
      </blockquote>
      <div class="layui-form-item">

        <div class="layui-inline">
          <input type="text" name="name" placeholder="名称" autocomplete="off" class="layui-input">
        </div>


        <div class="layui-inline">
          <select name="quick_id" lay-search>
            <option value="">选择App</option>
            {foreach $qas as $vo}
            <option value="{$vo.id}">{$vo.name}</option>
            {/foreach}
          </select>
        </div>

        <div class="layui-inline">
          <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
          </button>
        </div>
      </div>
    </div>
    <div class="layui-card-body">

      <table id="LAY-pop" lay-filter="LAY-pop"></table>


      <script type="text/html" id="table-useradmin-admin">
        <a class="layui-btn layui-btn-primary layui-border-blue layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
        <a class="layui-btn layui-btn-primary layui-border-red layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-edit"></i>删除</a>
      </script>

      <script type="text/html" id="toolbarDemo">
        <div class="layui-btn-container">
          <button class="layui-btn layui-btn-sm" lay-event="add">添加下载规则</button>
        </div>
      </script>

      <script type="text/html" id="switchTpl">
        <input type="checkbox" value="{{d.id}}" lay-skin="switch" lay-text="开|关" lay-filter="statusSwitch" {{ d.status == 1 ? 'checked' : '' }}>
      </script>

    </div>
  </div>
</div>


<script src="/static/layuiadmin/layui/layui.js"></script>

<script>
  layui.config({
    base: '/static/layuiadmin/' //静态资源所在路径
  }).extend({
    index: 'lib/index' //主入口模块
  }).use(['index','table'], function(){
    var $ = layui.$
            ,form = layui.form
            ,table = layui.table
            ,admin = layui.admin;

    //监听搜索
    form.on('submit(LAY-user-back-search)', function(data){
      var field = data.field;

      //执行重载
      table.reload('LAY-pop', {
        where: field
        ,page: {
          curr: 1  // 将页码重置为第一页
        }
      });
    });

    table.render({
      elem: '#LAY-pop'
      , url: '/popup/appPopups' //模拟接口
      , response: {
        statusCode: 1
      }
      , toolbar: '#toolbarDemo'
      , defaultToolbar: false
      , page: true //开启分页
      , cols: [[
        {field: 'id', width: 80, title: 'ID'}
        , {field: 'name', title: '名称'}
        , {field: 'quick_apps', title: 'App'}
        , {field: 'file_url', title: '源文件链接'}
        , {field: 'suc_count', title: '<span lay-tips="成功下载文件的数量，不代表已安装">下载数</span>', width: 150}
        , {field: 'status', title: '开关', width: 85, templet: '#switchTpl', unresize: true, align: 'center'}
        , {field: 'wgt', title: '权重', align: 'center', width: 150,edit: 'text', sort: true}
        , {field: 'create_time', title: '创建时间'}
        , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
      ]]
    });


    //监听单元格编辑
    table.on('edit(LAY-pop)', function (obj) { //注：edit是固定事件名，test是table原始容器的属性 lay-filter="对应的值"
      admin.req({
        url: '/popup/setAppPopWgt'
        , type: "POST"
        , data: {'id':  obj.data.id, 'wgt': obj.value}
        , done: function (res) {
          layer.msg('设置成功', {icon: 1, time: 1000}, function () {
          });
        }, not_finish: function (res) {
          layer.msg(res.msg);
        }
      });
    });



    //触发排序事件
    table.on('sort(LAY-pop)', function(obj){ //注：sort 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
      console.log(obj.field); //当前排序的字段名
      console.log(obj.type); //当前排序类型：desc（降序）、asc（升序）、null（空对象，默认排序）
      console.log(this); //当前排序的 th 对象

      //尽管我们的 table 自带排序功能，但并没有请求服务端。
      //有些时候，你可能需要根据当前排序的字段，重新向服务端发送请求，从而实现服务端排序，如：
      table.reload('LAY-pop', {
        initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。
        ,where: { //请求参数（注意：这里面的参数可任意定义，并非下面固定的格式）
          order: 'wgt ' + obj.type //排序方式
        }
        ,page: {
          curr: 1  // 将页码重置为第一页
        }
      });
      //layer.msg('服务端排序。order by '+ obj.field + ' ' + obj.type);
    });



    form.on('switch(statusSwitch)', function (data) {
      var status = 1;
      if (data.elem.checked === false) {
        var status = 0;
      }
      admin.req({
        url: '/popup/setAppPopStatus'
        , type: "POST"
        , data: {'id': data.value, 'status': status}
        , done: function (res) {
          layer.msg('设置成功', {icon: 1, time: 1000}, function () {
          });
        }, not_finish: function (res) {
          layer.msg(res.msg);
        }
      });
    });



    //监听行工具事件
    table.on('tool(LAY-pop)', function (obj) {
      var data = obj.data;
      if (obj.event === 'del') {
        layer.confirm('【'+obj.data.name+'】确认删除吗？',{icon: 3, title:false}, function(index){
          admin.req({
            url: '/popup/delAppPop'
            , type: "POST"
            , data: {'id': data.id}
            , done: function (res) {
              layer.msg('删除成功', {icon: 1, time: 1000}, function () {
                obj.del();
                layer.close(index);
              });
            }, not_finish: function (res) {
              layer.msg(res.msg);
            }
          });
        });
      } else if (obj.event === 'edit') {
        layer.open({
          type: 2
          , maxmin: true//开启最大化最小化按钮
          , title: '编辑规则'
          , content: '/popup/editAppPop.html?id=' + obj.data.id
          , offset: '5px'
          , area: ['840px', '666px']
          , btn: ['保存', '取消']
          , yes: function (index, layero) {
            var iframeWindow = window['layui-layer-iframe' + index]
                    , submit = layero.find('iframe').contents().find("#LAY-user-role-submit");
            //监听提交
            iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function (data) {
              var field = data.field; //获取提交的字段
              //提交 Ajax 成功后，静态更新表格中的数据
              //$.ajax({});
              admin.req({
                url: '/popup/editAppPop?id=' + obj.data.id
                , type: "POST"
                , data: field
                , done: function (res) {
                  layer.msg('保存成功', {icon: 1, time: 2000}, function () {
                    table.reload('LAY-pop');
                    layer.close(index); //关闭弹层
                  });
                }, not_finish: function (res) {
                  layer.msg(res.msg);
                }
              });
            });

            submit.trigger('click');
          }
        });


      }
    });




    //头工具栏事件
    table.on('toolbar(LAY-pop)', function (obj) {
      switch (obj.event) {
        case 'add':
          layer.open({
            type: 2
            ,maxmin: true//开启最大化最小化按钮
            ,title: '添加规则'
            ,content: '/popup/addAppPop.html'
            ,offset: '5px'
            ,area: ['840px', '666px']
            ,btn: ['保存', '取消']
            ,yes: function(index, layero){
              var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submit = layero.find('iframe').contents().find("#LAY-user-role-submit");

              //监听提交
              iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data){
                var field = data.field; //获取提交的字段
                //提交 Ajax 成功后，静态更新表格中的数据
                //$.ajax({});
                admin.req({
                  url: '/popup/addAppPop'
                  , type: "POST"
                  , data: field
                  , done: function (res) {
                    layer.msg('添加成功', {icon: 1, time: 2000}, function () {
                      table.reload('LAY-pop');
                      layer.close(index); //关闭弹层
                    });
                  },not_finish:function (res) {
                    layer.msg(res.msg);
                  }
                });
              });

              submit.trigger('click');
            }
          });
          break;
      }
    });
  });
</script>


</body>
</html>

